//index.js
//获取应用实例
const app = getApp();
var util = require("../../utils/util");
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
Page({
  data: {
    bannerData: "", //轮播图
    goodDataList: "", //精选歌单
    topData: "", //点唱榜
    reachTop: false, //滑动到顶部按钮显示
    authorShow: false, //显示授权弹框
    wxImg: "", //微信头像
    wxName: "", //微信昵称
  },
  // 轮播、歌单点击进入歌单详情
  swiperClick(e) {
    console.log(e.currentTarget.dataset);
    if (e.currentTarget.dataset.category == 2) {
      wx.navigateTo({
        url:
          "../my/helpDetail/helpDetail?id=" +
          e.currentTarget.dataset.id +
          "&from=" +
          "banner",
      });
    } else {
      app.globalData.gedanData = e.currentTarget.dataset;
      wx.navigateTo({
        url: "../songListDetail/songListDetail?from=" + "banner",
      });
    }
  },
  // 歌单点击进入歌单详情
  itemClick(e) {
    console.log(e.currentTarget.dataset);
    app.globalData.gedanData = e.currentTarget.dataset;
    wx.navigateTo({
      url: "../songListDetail/songListDetail",
    });
  },
  onLoad: function () {
    var that = this;
    var token = wx.getStorageSync("token");
    that.setData({
      token: token,
    });
    wx.getStorage({
      key: "wxImg",
      success(res) {
        console.log(res.data, "缓存");
        that.setData({
          wxImg: res.data,
        });
      },
    });
    wx.getStorage({
      key: "wxName",
      success(res) {
        console.log(res.data, "缓存");
        that.setData({
          wxName: res.data,
        });
      },
    });
    // 轮播图
    util.postRes("/topics/topicstype", { type: 11 }, function (res) {
      // console.log(res)
      if (res.status_code == 200) {
        console.log(res.data.childrens);
        let bannerData = [];
        let lunboData = res.data.childrens;
        for (let index in lunboData) {
          let picbannerData = {
            img: lunboData[index].spreadimg
              ? lunboData[index].spreadimg
              : "/images/swiper1.png",
            id: lunboData[index].id,
            category: lunboData[index].category,
            name: lunboData[index].name,
            type: lunboData[index].type,
          };
          bannerData.push(picbannerData);
          that.setData({
            bannerData: bannerData,
          });
        }
      } else {
      }
    });
    // 精选歌单
    util.postRes("/mobile/topmoduletopics", { type: 1 }, function (res) {
      if (res.status_code == 200) {
        res.data.splice(5, 4);
        that.setData({
          goodDataList: res.data,
        });
      }
    });
    //点唱榜
    util.postRes("/mobile/topmoduletopics", { type: 4 }, function (res) {
      if (res.status_code == 200) {
        for (let i in res.data) {
          // str.indexOf("3") != -1
          if (res.data[i]["name"].indexOf("点唱榜") != -1) {
            var topid = res.data[i]["topicsid"];
          }
        }
        var topurl = "/mobile/topicssong?pagesize=10&id=" + topid;
        util.postRes(topurl, { type: 2 }, function (res) {
          // console.log(res, "666666666");
          if (res.status_code == 200) {
            that.setData({
              topData: res.data.songlist,
            });
          }
        });
      }
    });
    // 微信授权
    if (that.data.wxImg || that.data.wxName) {
    } else {
      // 查看是否授权
      wx.getSetting({
        success(res) {
          console.log(res.authSetting);
          if (res.authSetting["scope.userInfo"]) {
            console.log("已经授权，没有缓存");
            wx.getUserInfo({
              success: function (res) {
                wx.setStorageSync("wxImg", res.userInfo.avatarUrl);
                wx.setStorageSync("wxName", res.userInfo.nickName);
                that.setData({
                  wxImg: res.userInfo.avatarUrl,
                  wxName: res.userInfo.nickName,
                  authorShow: false,
                });
              },
            });
          } else {
            that.setData({
              authorShow: true,
            });
          }
        },
      });
    }
  },
  // 播放歌曲
  play(e) {
    var that = this;
    console.log(e.currentTarget.dataset.code);
    var token = wx.getStorageSync("token");
    var code = e.currentTarget.dataset.code;
    var tempParam = {
      code: code,
    };
    if (token) {
      util.postToken("/wechat/play", tempParam, token, function (res) {
        console.log(res, "播放");
        if (res.status_code == 200) {
          Toast.success('成功');
        } else if(res.status_code == 1002) {
          console.log(res)
          // 播放歌曲接口调用websocket服务出错：ch==null,设备没经过握手
          Toast('电视未连接');
        }else{
          // Toast(res.msg);
        }
      });
    }else{
      Toast('请先连接电视');
    }
  },
  // 添加歌曲
  add(e) {
    var that = this;
    var codes = e.currentTarget.dataset.code;
    // "fa15884d-0ebb-388e-b179-363a077e089f"
    var uid = app.globalData.uid;
    // var uid = wx.getStorageSync("uid");
    var token = wx.getStorageSync("token");
    var headurl = wx.getStorageSync("wxImg");
    // console.log("avatarUrl:", wx.getStorageSync("avatarUrl"));
    // var headeimg = wx.getStorageSync("avatarUrl");
    var tempParam = {
      codes: codes,
      uid: uid,
      headurl: headurl,
    };
    if (token) {
      util.postToken("/wechat/playlist/song/add", tempParam, token, function (
        res
      ) {
        if (res.status_code == 200) {
          Toast.success('成功');
        } else if(res.status_code == 1002) {
          console.log(res)
          Toast(res.msg);
        }else{
          Toast(res.msg);
        }
        console.log(res);
      });
    } else {
    }
  },
  // 收藏歌曲
  love(e) {
    var that = this;
    var songCode = e.currentTarget.dataset.code;
    var token = wx.getStorageSync("token");
    var tempParam = {
      songCode: songCode,
    };
    if (token) {
      util.postToken("/wechat/song/favorites/add", tempParam, token, function (
        res
      ) {
        console.log(res);
        if (res.status_code == 200) {
          Toast.success('收藏成功');
        } else if(res.status_code == 1002) {
          console.log(res)
          Toast(res.msg);
        }else{
          Toast(res.msg);
        }
      });
    }
  },
  gotosingerList: function (params) {
    wx.navigateTo({
      url: "../singerList/singerList",
    });
  },
  gotoSearch() {
    wx.navigateTo({
      url: "../search/search",
    });
  },
  gotosongType: function (params) {
    wx.navigateTo({
      url: "../songType/songType",
    });
  },
  gotoMoodlist() {
    wx.navigateTo({
      url: "../moodList/moodList",
    });
  },
  gotogedanGuangchang: function (params) {
    wx.navigateTo({
      url: "../songListSquare/index/index",
    });
  },
  gototopList: function (params) {
    wx.navigateTo({
      url: "../topList/topList",
    });
  },
  // 已点列表
  gotoyidianList: function (params) {
    wx.navigateTo({
      url: "../alreadyList/alreadyList",
    });
  },
  // 页面滚动回到顶部
  gotoTop() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300,
    });
  },
  // 微信授权
  getUserInfo(res) {
    console.log(res.detail.userInfo.avatarUrl, "微信头像");
    console.log(res.detail.userInfo.nickName, "微信昵称");
    wx.setStorageSync("wxImg", res.detail.userInfo.avatarUrl);
    wx.setStorageSync("wxName", res.detail.userInfo.nickName);
    this.setData({
      wxImg: res.detail.userInfo.avatarUrl,
      wxName: res.detail.userInfo.nickName,
      authorShow: false,
    });
  },
  // 关闭授权弹框
  closeAuthor() {
    this.setData({
      authorShow: false,
    });
  },
  // 监听页面滚动，获取scrollTop值
  onPageScroll: function (params) {
    // console.log(params.scrollTop)

    if (params.scrollTop > 300) {
      this.setData({
        reachTop: true,
      });
    } else if (params.scrollTop < 300) {
      this.setData({
        reachTop: false,
      });
    }
  },
});
